package com.huilian.cloud.module.system.controller.admin.datascope;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.huilian.cloud.framework.common.pojo.PageParam;
import com.huilian.cloud.framework.common.pojo.PageResult;
import com.huilian.cloud.framework.common.pojo.CommonResult;
import com.huilian.cloud.framework.common.util.object.BeanUtils;
import static com.huilian.cloud.framework.common.pojo.CommonResult.success;

import com.huilian.cloud.framework.excel.core.util.ExcelUtils;

import com.huilian.cloud.framework.apilog.core.annotation.ApiAccessLog;
import static com.huilian.cloud.framework.apilog.core.enums.OperateTypeEnum.*;

import com.huilian.cloud.module.system.controller.admin.datascope.vo.*;
import com.huilian.cloud.module.system.dal.dataobject.datascope.DatascopeDO;
import com.huilian.cloud.module.system.service.datascope.DatascopeService;

@Tag(name = "管理后台 - 数据权限")
@RestController
@RequestMapping("/sys/datascope")
@Validated
public class DatascopeController {

    @Resource
    private DatascopeService datascopeService;

    @PostMapping("/create")
    @Operation(summary = "创建数据权限")
    @PreAuthorize("@ss.hasPermission('sys:datascope:create')")
    public CommonResult<Integer> createDatascope(@Valid @RequestBody DatascopeSaveReqVO createReqVO) {
        return success(datascopeService.createDatascope(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据权限")
    @PreAuthorize("@ss.hasPermission('sys:datascope:update')")
    public CommonResult<Boolean> updateDatascope(@Valid @RequestBody DatascopeSaveReqVO updateReqVO) {
        datascopeService.updateDatascope(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据权限")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sys:datascope:delete')")
    public CommonResult<Boolean> deleteDatascope(@RequestParam("id") Integer id) {
        datascopeService.deleteDatascope(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据权限")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sys:datascope:query')")
    public CommonResult<DatascopeRespVO> getDatascope(@RequestParam("id") Integer id) {
        DatascopeDO datascope = datascopeService.getDatascope(id);
        return success(BeanUtils.toBean(datascope, DatascopeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据权限分页")
    @PreAuthorize("@ss.hasPermission('sys:datascope:query')")
    public CommonResult<PageResult<DatascopeRespVO>> getDatascopePage(@Valid DatascopePageReqVO pageReqVO) {
        PageResult<DatascopeDO> pageResult = datascopeService.getDatascopePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DatascopeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据权限 Excel")
    @PreAuthorize("@ss.hasPermission('sys:datascope:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDatascopeExcel(@Valid DatascopePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DatascopeDO> list = datascopeService.getDatascopePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数据权限.xls", "数据", DatascopeRespVO.class,
                        BeanUtils.toBean(list, DatascopeRespVO.class));
    }

}